package com.fesine.quartz.chapter7;

import com.fesine.quartz.chapter4.PrintJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;
import java.util.Date;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/19 20:50
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/19 20:50
 */
public class Listing_7_7 {
    static Log logger = LogFactory.getLog(Listing_7_7.class);

    public static void main(String[] args) {
        new Listing_7_7().startScheduler();
    }

    private void startScheduler() {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            String jobName = PrintJob.class.getSimpleName();
            JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, PrintJob
                    .class);
            TriggerListener listener = new SimpleTriggerListener();
            //注册全局triggerListener
            //scheduler.addGlobalTriggerListener(listener);
            //注册非全局 1添加scheduler.addTriggerListener
            scheduler.addTriggerListener(listener);
            Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);
            trigger.setName("simpleTrigger");
            trigger.setGroup("simpleTriggerGroup");
            trigger.setStartTime(new Date());
            //2在trigger上添加addTriggerListener
            trigger.addTriggerListener(listener.getName());
            scheduler.deleteJob(jobName, Scheduler.DEFAULT_GROUP);
            scheduler.scheduleJob(jobDetail, trigger);
            CronTrigger trigger1 = new CronTrigger("cronTrigger", "cronTriggerGroup", "0/15 " +
                    "* * ? * *");
            jobDetail.setGroup("cronGroup");
            scheduler.deleteJob(jobName, "cronGroup");
            scheduler.scheduleJob(jobDetail, trigger1);
            scheduler.start();
            logger.info("scheduler starting up...");
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
